package com.cola.xml.JSON;

import com.google.gson.stream.JsonReader;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * @Grup:com.cola.xml.JSON
 * @Auther:cola
 * @Data:2021/4/12-2021-04-0:38
 * @version:1.0
 */
public class JSONDemo {
    /*
    * 解析一个JSON数组
    * */
    @Test
    public void parseJSONNames(){
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("names.json");
        InputStreamReader in = new InputStreamReader(is);
        //JSON的解析工具(解析器)
        JsonReader reader=new JsonReader(in);
        ArrayList<Name> list=new ArrayList<>();
        try {
            //开始解析json数组
            reader.beginArray();
            while (reader.hasNext()){
                list.add(parseName(reader));
            }
            //结束解析数组
            reader.endArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(Arrays.toString(list.toArray()));
    }
    //解析对象 Name
    private Name parseName(JsonReader jsonReader){
        Name name=null;
        try {
            //开始解析对象
            jsonReader.beginObject();
            name=new Name();
            while (jsonReader.hasNext()){
                String attrName = jsonReader.nextName();
                if ("firstName".equals(attrName)){
                    name.setFirstName(jsonReader.nextString());
                }else if ("lastName".equals(attrName)){
                    name.setLastName(jsonReader.nextString());
                }else if ("email".equals(attrName)){
                    name.setEmail(jsonReader.nextString());
                }
            }
            //结束解析对象
            jsonReader.endObject();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return name;
    }
}
